import { createStore } from 'vuex'

export default createStore({
  state: {
    // 读取浏览器缓存的登录信息
    cur_user: sessionStorage.getItem('user') == null ? null : JSON.parse(sessionStorage.getItem('user')),
  },
  getters: {
    nickName: state => {
      // 读取用户名：如果用户不存在则为空，否则读取用户的昵称
      return state.cur_user == null ? '访客' : state.cur_user.nickName;
    },
    useImg: state => {
      // 读取用户头像：如果用户不存在则为空，否则读取用户的头像
      return (state.cur_user == null || state.cur_user.img == null || state.cur_user.img == '')
        ? "https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png"
        : "http://localhost:9000/images/user/" + state.cur_user.img;
    },
  },
  mutations: {
    addUser(state, user) {
      state.cur_user = user
    },
    delUser(state) {
      state.cur_user = null

    },
  },
  actions: {
  },
  modules: {
  }
})
